<?php

namespace App\Http\Resources\OpenShop\Oranization\Web;

use App\Http\Resources\BaseResource;
use App\Models\OpenShop\Organization\OpenShopRegionManageMappingModel;
use App\Models\OpenShop\Organization\OpenShopRegionManageUserMappingModel;
use Illuminate\Http\Request;

class WazZoneListResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            "id"                => $this->id,
            "name"              => $this->name,
            "regionMappingRel"  => $this->regionMappingRel->map(function (
                OpenShopRegionManageMappingModel $manageMappingModel
            ) {
                return [
                    "id"   => $manageMappingModel->manageId,
                    "name" => attributeGet($manageMappingModel, "organizationManageRel.name", "")
                ];
            }),
            "chargeUsers"       => $this->regionUserMappingRel->where("type",
                OpenShopRegionManageUserMappingModel::USER_TYPE_1)->values(),
            "attachChargeUsers" => $this->regionUserMappingRel->where("type",
                OpenShopRegionManageUserMappingModel::USER_TYPE_2)->values(),
            "updatedAtText"     => $this->updatedAt->format('Y-m-d H:i:s')
        ];
    }
}
